<template>
	<view class="app">

		<view class="column center">
			<view>
				<image class="imag-logo" src="/static/images/logo @2x.png"></image>
			</view>
			<view class="logo-v1">
				<text class="logo-text1">孕道小程序</text>
			</view>
			<view class="logo-v2">
				<text class="logo-text2">怀孕备孕就上孕道</text>
			</view>
		</view>
		<view class="center">
			<button class="input-content" :loading="loading" type="primary" v-if="showp"
				@click="getuserinfo">登录</button>

			<button class="input-content" :loading="loading" type="primary" open-type="getPhoneNumber"
				@getphonenumber="getphone" v-else >获取电话</button>
		</view>




	</view>
</template>

<script>
	import api from '@/api/auth.js'

	export default {
		data() {
			return {
				iv: 6,
				userinfo: '',
				showp: true,
				session_key: '',
				phoneinfo: '',
				encryptedData: '',
				infoRes: '',
				superior: 0,
				code: '',
				openid: '',
				loading: false, // 提交中

			}
		},
		onShow() {
			//获取code
			let that = this
			uni.login({
				success: function(loginRes) {
					that.code = loginRes.code
				},
				fail: function(err) {
					console.log(err)
				}
			})

		},
		methods: {

			getphone(e) {
console.log(e)
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					this.phoneinfo = e.detail
					uni.showLoading({
						title: '登录中...',
						mask: true
					});
					// _this.getopenid()
				}



			},

			getuserinfo() {
				let _this = this;
				uni.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (infoRes) => {
						console.log(infoRes)
						_this.userinfo = infoRes.userInfo,
							_this.infoRes = infoRes
						_this.showp = false
					},
					fail: function(res) {
						uni.hideLoading();
					}
				})
			},
			// 
			async getopenid() {
				const {
					code,
					message,
					data
				} = await api.loginByProvider({
					code: this.code
				})
				if (code == 1) {
					this.session_key = data.session_key
					this.openid = data.openid

				}

			},


			// 第3方授权登录
			toProviderLogin(provider) {

				uni.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (infoRes) => {

						// console.log(infoRes)
						this.iv = infoRes.iv
						this.encryptedData = infoRes.encryptedData



					},
					fail: function(res) {
						uni.hideLoading();
					}
				})

				uni.showLoading({
					mask: true
				})


				uni.login({
					provider,

					success: (res) => {
						console.log('授权成功', res)

						// #ifdef MP-WEIXIN
						console.log('yyy')
						const data = {
							code: res.code
						}
						// #endif
						// 获取用户信息




						// 2. 授权成功后，请求自己的后台认证接口来完成自己平台的认证
						this.getServiceUserInfo(data)

					},
					fail: (err) => {
						this.$util.msg('授权登录失败')
						uni.hideLoading()
					}
				})
			},


			// 获取应用内的认证授权信息
			async getServiceUserInfo(reqData) {
				let _this = this;
				// 1. 调用后台来完成登录
				const {
					code,
					message,
					data
				} = await api.loginByProvider(reqData)
				uni.hideLoading()

				if (code != 1) {

					this.$util.msg(message)
					return
				}

				let aa = data.session_key

				if (data && data.session_key) {
					console.log(data)

					_this.session_key = aa
					return
					// _this.session_key=data.session_key
					// console.log(data.session_key)
					// console.log('fff')

					// console.log(data)
					// that.session_key = data.session_key
					// that.openid = data.openid

					// _this.userinfo=data.session_key,
					// console.log(data.session_key)
					// console.log(data.session_key)
					// console.log(that.session_key)
					// console.log(data.openid)
					// console.log(that.openid)
					console.log('fff')



					const {
						code,
						message,
						data
					} = await api.insetData({
						openid: this.openid,
						session_key: this.session_key,
						iv: this.iv,
						encryptedData: this.encryptedData,
						superior: this.superior
					})
					console.log(111999)
					console.log('data')
					console.log(2229999)
					// 登录成功 
					this.loginSuccessCallBack(data)
				} else {
					// 未绑定手机号，进入绑定手机号页面
					this.$util.msg('登录失败')
					this.navTo('/pages/auth/login')
				}

			},


			// 登录成功操作 vuex 
			loginSuccessCallBack(data) {

				this.$util.msg('登录成功')
				// 状态管理保存用户信息
				this.$store.commit('setToken', data)
				setTimeout(() => {
					this.navBack()
				}, 500)
			},
		}
	}
</script>

<style lang="scss">
	.app {
		padding-top: 416rpx;
	}

	.imag-logo {
		height: 200rpx;
		width: 200rpx;

		margin-bottom: 20rpx
	}

	.logo-v1 {
		margin-bottom: 20rpx
	}

	.logo-text1 {
		width: 179rpx;
		height: 36rpx;
		font-size: 36rpx;
		font-family: Microsoft YaHei;
		font-weight: bold;
		color: #643A92;

	}

	.logo-v2 {
		margin-bottom: 20rpx
	}

	.logo-text2 {
		width: 193rpx;
		height: 25rpx;
		font-size: 24rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #999999;
	}

	.input-content {
		width: 610rpx;
		height: 100rpx;
		background: linear-gradient(90deg, #BB73D5, #643A92);
		box-shadow: 0rpx 4rpx 6rpx 0rpx rgba(100, 58, 146, 0.4);
		border-radius: 50rpx;
		text-align: center;
		font-size: 35rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 120rpx;

	}
</style>
